package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.hotel.saas.core.dao.OutletsDAO;
import com.ecoolex.hotel.saas.entity.Outlets;
import com.ecoolex.hotel.saas.entity.Outlets.Column;
import com.ecoolex.hotel.saas.entity.OutletsExample;
import com.ecoolex.hotel.saas.entity.OutletsExample.Criteria;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OutletsManager {
	
    @Autowired
    private OutletsDAO dao;
    
    @Autowired
    private DepartmentOutletsJointManager jointManager;
    
    public Outlets getOutlets(Integer outletsId, Integer storeId) {
    	OutletsExample example = new OutletsExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andOutletsIdEqualTo(outletsId).andStoreIdEqualTo(storeId).andDeletedEqualTo(Outlets.NOT_DELETED);
		return dao.selectOneByExample(example);
	}

	public Pagination<Outlets> queryOutlets(String name, Integer storeId, Page page) {
		OutletsExample example = new OutletsExample();
    	Criteria criteria = example.createCriteria();
    	if(Check.notNullOrEmpty(name)) {
    		criteria.andNameLike("%"+name+"%");
    	}
    	criteria.andStoreIdEqualTo(storeId).andDeletedEqualTo(Outlets.NOT_DELETED);
    	example.setOffset(page.getFirstIndex());
    	example.setRows(page.getPageSize());
    	example.orderBy(Column.createTime.desc());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}

	public Outlets getByStoreIdAndName(Integer storeId, String name){
		OutletsExample example = new OutletsExample();
		Criteria criteria = example.createCriteria();
		criteria.andStoreIdEqualTo(storeId).andNameEqualTo(name).andDeletedEqualTo(Outlets.NOT_DELETED);
		return dao.selectOneByExample(example);
	}

	public Outlets addOutlets(Outlets add, List<Integer> departmentIdList, String createUser){
		BizAssert.isTrue(Check.isNull(getByStoreIdAndName(add.getStoreId(),add.getName())), ResultCode.BIZ.build(10), "营业点名称不能重复");
		add.setDeleted(Outlets.NOT_DELETED);
		add.setCreateUser(createUser);
		add.setCreateTime(Date.from(Instant.now()));
		add.setUpdateUser(createUser);
		add.setUpdateTime(Date.from(Instant.now()));
		dao.insert(add);
		jointManager.update(add.getOutletsId(), departmentIdList, createUser);
		return add;
	}

	public Outlets getByStoreAndNameNotOutletsId(Integer storeId, String name, Integer outletsId){
		OutletsExample example = new OutletsExample();
		Criteria criteria = example.createCriteria();
		criteria.andOutletsIdNotEqualTo(outletsId).andStoreIdEqualTo(storeId).andNameEqualTo(name).andDeletedEqualTo(Outlets.NOT_DELETED);
		return dao.selectOneByExample(example);
	}

	public Outlets editOutlets(Outlets edit, List<Integer> departmentIdList, String updateUser){
		BizAssert.isTrue(Check.isNull(getByStoreAndNameNotOutletsId(edit.getStoreId(),edit.getName(),edit.getOutletsId())), ResultCode.BIZ.build(10), "营业点名称不能重复");
		edit.setUpdateUser(updateUser);
		edit.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(edit, Column.excludes(Column.deleted,Column.storeId,Column.createUser,Column.createTime));
		jointManager.update(edit.getOutletsId(), departmentIdList, updateUser);
		return edit;
	}

	public Boolean delOutlets(Outlets edit, String updateUser){
		edit.setDeleted(Outlets.IS_DELETED);
		edit.setUpdateUser(updateUser);
		edit.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(edit, Column.deleted,Column.updateUser,Column.updateTime);
		return true;
	}
}